package com.dj.util;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.dj.pojo.Button;
import com.dj.pojo.ClickButton;
import com.dj.pojo.Image;
import com.dj.pojo.ImageMessage;
import com.dj.pojo.Menu;
import com.dj.pojo.News;
import com.dj.pojo.NewsMessage;
import com.dj.pojo.TextMessage;
import com.dj.pojo.ViewButton;

/**
 * 初始化值
 * @author Jon
 *
 */
public class InitUtil {
	/**
	 * 组装菜单
	 * @return
	 */
	public static Menu initMenu() {

		Menu menu = new Menu();
		ClickButton button11 = new ClickButton();
		button11.setName("美女帅哥");
		button11.setType("click");
		button11.setKey("11");
		
		ClickButton button12 = new ClickButton();
		button12.setName("自然风景");
		button12.setType("click");
		button12.setKey("12");
		
		ViewButton button21 = new ViewButton();
		button21.setName("战争片");
		button21.setType("view");
		button21.setUrl("http://www.baidu.com");
		
		ViewButton button22 = new ViewButton();
		button22.setName("科幻片");
		button22.setType("view");
		button22.setUrl("http://video.duowan.com/play/8441833.html");
		
		ViewButton button3 = new ViewButton();
		button3.setName("个人信息测试");
		button3.setType("view");
		button3.setUrl("https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx90902aef5a1fe7e0&redirect_uri=http%3A%2F%2F9bruti.natappfree.cc%2Fdj-weixin%2FgetUserInfo&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect");
		
		Button button1 = new Button();
		button1.setName("精美图片"); // 将11/12两个button作为二级菜单封装第一个一级菜单
		button1.setSub_button(new Button[] { button11, button12 });
		Button button2 = new Button();
		button2.setName("精彩视频"); // 将21/22两个button作为二级菜单封装第二个二级菜单
		button2.setSub_button(new Button[] { button21, button22 });
		menu.setButton(new Button[] { button1, button2, button3 });// 将31Button直接作为一级菜单
		return menu;
	}
	
	/**
	 * 
	 * 拼接关注主菜单
	 * 
	 */

	public static String menuText() {
		String menu = "欢迎关注公众号，请选择:\n\n"
				+ "1、帅哥服务。\n"
				+ "2、美女服务。\n"
				+ "3、详情信息。\n"
				+ "4、美女图片。\n\n"
				+ "回复？调出主菜单。";
		return menu;
	}

	/**
	 * 
	 * 初始化回复消息
	 * 
	 */
	public static String initText(String toUSerName, String fromUserName, String content) {
		TextMessage text = new TextMessage();
		text.setFromUserName(toUSerName);// 原来【接收消息用户】变为回复时【发送消息用户】
		text.setToUserName(fromUserName);
		text.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_TEXT);
		text.setCreateTime(new Date().getTime());
		text.setContent(content);
		return MessageUtil.objectToXml(text);
	}
	
	/**
     * 初始化图文消息
     */
    public static String initNewsMessage(String toUSerName,String fromUserName){
        List<News> newsList = new ArrayList<News>();
        NewsMessage newsMessage = new NewsMessage();
        //组建一条图文↓ ↓ ↓
        News newsItem = new News();
        newsItem.setTitle("欢迎来jon空间");
        newsItem.setDescription("领先中国挚爱排行！！！");
        newsItem.setPicUrl("https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=3878847766,3988120331&fm=200&gp=0.jpg");
        newsItem.setUrl("www.baidu.com");
        newsList.add(newsItem);
        
        //组装图文消息相关信息
        newsMessage.setToUserName(fromUserName);
        newsMessage.setFromUserName(toUSerName);
        newsMessage.setCreateTime(new Date().getTime());
        newsMessage.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_NEWS);
        newsMessage.setArticles(newsList);
        newsMessage.setArticleCount(newsList.size());
        
        //调用newsMessageToXml将图文消息转化为XML结构并返回
        return MessageUtil.newsMessageToXml(newsMessage);
    }
    
    /**
     * 组装图片消息
     * @param toUSerName
     * @param fromUserName
     * @return
     */
    public static String initImageMessage(String toUSerName,String fromUserName) {
    	String message = null;
    	Image image = new Image();
    	image.setMediaId("FhnZ6Z2kHXZ5alyE4Pa3i7FBgVyx_lPpW9jcKo9NHWELQ5OlWiRFtWbxntuz3YsU");
    	ImageMessage imageMessage = new ImageMessage();
    	imageMessage.setFromUserName(toUSerName);
    	imageMessage.setToUserName(fromUserName);
    	imageMessage.setMsgType(MessageUtil.REQ_MESSAGE_TYPE_IMAGE);
    	imageMessage.setCreateTime(new Date().getTime());
    	imageMessage.setImage(image);
    	message = MessageUtil.imageMessageXml(imageMessage);
    	return message;
    }
    
    /**
	 * 初始化点击事件消息回复
	 */
	public static String initEvnet(String toUSerName, String fromUserName, String eventKey) {
		TextMessage text = new TextMessage();
		text.setFromUserName(toUSerName);// 原来【接收消息用户】变为回复时【发送消息用户】
		text.setToUserName(fromUserName);
		text.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_TEXT);
		text.setCreateTime(new Date().getTime());
		String content = "";
		if ("11".equals(eventKey)) {
			content = "你点击了美女帅哥";
		} else if ("12".equals(eventKey)) {
			content = "你点击了自然风景";
		} else if ("3".equals(eventKey)) {
			content = "请自行解决";
		}
		text.setContent(content);
		return MessageUtil.objectToXml(text);
	}
}
